package org.example.struct.array;

import java.util.HashSet;
import java.util.Set;

/**
 * @author liyishan
 * @date 2024/2/1 09:52
 * @apiNote
 */

public class Test00929NumUniqueEmails {
    public static void main(String[] args) {
        String[] emails = {"test.email+alex@leetcode.com","test.e.mail+bob.cathy@leetcode.com","testemail+david@lee.tcode.com"};
        System.out.println(numUniqueEmails(emails));
        //String email = "test.email+alex@leetcode.com";
        //System.out.println(convertEmail(email));
    }

    public static int numUniqueEmails1(String[] emails) {
        Set<String> emailSet = new HashSet<String>();
        for (String email : emails) {
            int i = email.indexOf('@');
            String local = email.substring(0, i).split("\\+")[0]; // 去掉本地名第一个加号之后的部分
            local = local.replace(".", ""); // 去掉本地名中所有的句点
            emailSet.add(local + email.substring(i));
        }
        return emailSet.size();
    }


    public static int numUniqueEmails(String[] emails) {
        Set<String> set = new HashSet<>();
        for(int i = 0; i < emails.length; i++){
            String email = convertEmail(emails[i]);
            set.add(email);
        }
        return set.size();
    }

    public static String convertEmail(String email) {
        String local = email.substring(0,email.indexOf("@"));
        String domain = email.substring(email.indexOf("@"), email.length());
        if(local.contains(".")){
            local = local.replaceAll("\\.","");
        }
        if(local.contains("+")){
            local = local.substring(0, local.indexOf("+"));
        }
        return local+domain;
    }
}
